package com.skivingcloud.camunda.service.impl;

import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.camunda.service.ModelService;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.StartEvent;
import org.camunda.bpm.model.xml.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 流程模型管理
 */
@Service
public class ModelServiceImpl implements ModelService{
    
    @Autowired
    private RepositoryService repositoryService;
    
    /**
     * 分页查询模型
     *
     * @param params 参数
     * @param user 用户
     * @return 流程列表
     */
    @Override
    public PageUtils list(Map<String, Object> params, User user){
        String searchName = params.get("searchName") == null ? "" : params.get("searchName").toString();
        int pagesize = Integer.parseInt(params.get("page").toString());
        int limit = Integer.parseInt(params.get("limit").toString());
        int offset = (pagesize - 1) * limit;
        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
        if(StringUtils.isNotBlank(searchName)){
            query = query.processDefinitionNameLike("%" + searchName + "%");
        }
        List<ProcessDefinition> list = query.latestVersion().tenantIdIn(user.getTenantId())
                .orderByDeploymentTime().desc().listPage(offset, limit);
        int total = (int) query.latestVersion().tenantIdIn(user.getTenantId()).count();
        return new PageUtils(list, total);
    }
    
    /**
     * 根据id加载模型数据
     *
     * @param processDefinitionId 流程定义id
     * @return 流程模型
     */
    @Override
    public BpmnModelInstance getEditorJson(String processDefinitionId) throws Exception{
        return repositoryService.getBpmnModelInstance(processDefinitionId);
    }
    
    /**
     * 获取最新版的流程定义
     *
     * @return 流程定义
     */
    @Override
    public List<ProcessDefinition> getProcessOption(){
        return repositoryService.createProcessDefinitionQuery().latestVersion().list();
    }
    
    /**
     * 发布模型
     *
     * @param id 模型id
     * @param user 用户
     * @throws Exception
     */
    @Override
    public void deploy(String id, User user) throws Exception{
        ProcessDefinition p = repositoryService.getProcessDefinition(id);
        InputStream inp = repositoryService.getProcessModel(id);
        BpmnModelInstance bpmnModelInstance = repositoryService.getBpmnModelInstance(id);
        repositoryService.createDeployment().name(bpmnModelInstance.getModel().getModelName()).tenantId(p.getTenantId()).addInputStream(p.getResourceName(),inp).deploy();
    }
    
    /**
     * 加载流程文件内容
     *
     * @param deploymentId
     * @param resType
     * @param processDefinition
     * @return
     */
    @Override
    public InputStream resourceRead(String deploymentId, String resType, ProcessDefinition processDefinition){
        return null;
    }
    
    /**
     * 流程导入
     *
     * @param files
     * @param user
     * @throws Exception
     */
    @Override
    public void improtDeploy(List<MultipartFile> files, User user) throws Exception{
    
    }
    
    /**
     * 将流程转化为模型
     *
     * @param procDefId
     * @return
     * @throws Exception
     */
    @Override
    public Model convertToModel(String procDefId) throws Exception{
        return null;
    }
    
    /**
     * 获取开始节点信息用于组织页面表单
     *
     * @param defKey
     * @return
     */
    @Override
    public StartEvent getModelStartNodeInfo(String defKey){
        return null;
    }
}
